// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   StringUtil.java

package com.iphaeton.common.util;

import java.io.UnsupportedEncodingException;
import java.util.*;
import org.apache.log4j.Logger;

public class StringUtil {

	public StringUtil() {
	}

	public static int StringNum(String str) {
		StringBuffer nums = new StringBuffer(20);
		double ret = 0.0D;
		char numStr[] = str.toCharArray();
		for (int i = 0; i < numStr.length; i++)
			if (hNum.containsKey(String.valueOf(numStr[i])))
				nums.append(numStr[i]);

		try {
			ret = Double.parseDouble(nums.toString());
		} catch (Exception e) {
			log.info("StringUtil->StringNum(String str):" + e.toString());
			ret = 0.0D;
		}
		return (int) Math.round(ret);
	}

	public static String iso2GBK(String str) {
		try {
			if (str != null)
				str = new String(str.getBytes("ISO8859-1"), "GBK");
			else
				str = "";
			return str;
		} catch (Exception e) {
			log.info("StringUtil->iso2GBK(String str):" + e.toString());
		}

		return "error";
	}

	public static int str2int(String val, int def) {
		try {
			int temp = Integer.parseInt(val);
			return temp;

		} catch (Exception e) {
			log
					.info("StringUtil->str2int(String val, int def):"
							+ e.toString());
		}

		return def;
	}

	public static String space2dot(String key, HashSet set) {
		StringBuffer str = new StringBuffer(1024);
		str.append("'");
		String temp;
		for (StringTokenizer token = new StringTokenizer(key, " "); token
				.hasMoreTokens(); set.add(temp)) {
			temp = token.nextToken();
			str.append(temp + "','");
		}

		return str.toString().substring(0, str.toString().length() - 2);
	}

	public static String filterADD(String key) {
		if (key == null)
			return "";
		if (key.indexOf("+") == -1 && key.indexOf("\uFF0B") == -1
				&& key.indexOf("\uFF0D") == -1 && key.indexOf("\u3000") == -1)
			return key;
		StringBuffer str = new StringBuffer(1024);
		char temp[] = key.toCharArray();
		for (int i = 0; i < temp.length; i++)
			if (temp[i] == '\uFF0D')
				str.append("-");
			else if (temp[i] == '\u3000')
				str.append(" ");
			else if (temp[i] != '+' && temp[i] != '\uFF0B')
				str.append(temp[i]);

		return str.toString();
	}

	public static int[] String2Divide(String str) {
		char val[] = str.toCharArray();
		int temp[] = new int[val.length];
		for (int i = 0; i < val.length; i++)
			temp[i] = val[i];

		return temp;
	}

	public static int[] String2Divide(String str, String sp) {
		String val[] = str.split(sp);
		int temp[] = new int[val.length];
		for (int i = 0; i < val.length; i++)
			if (val[i] == null || val[i].trim().equals(""))
				temp[i] = 0;
			else
				temp[i] = Integer.parseInt(val[i]);

		return temp;
	}

	public static String processStr(String str) {
		if (str == null || str.equals(""))
			return "";
		str = str + " ";
		StringBuffer strFinal = new StringBuffer(100);
		char charArray[] = str.toCharArray();
		boolean change = true;
		StringBuffer temp = null;
		for (int i = 0; i < charArray.length; i++) {
			if (i == 0)
				temp = new StringBuffer(20);
			if (strMap.containsKey(String.valueOf(charArray[i]))) {
				if (temp.length() == 1)
					strFinal.append(temp.toString()).append("*").append(
							charArray[i]);
				else
					strFinal.append(temp.toString()).append(charArray[i]);
				temp = new StringBuffer(20);
			} else {
				temp.append(charArray[i]);
			}
		}

		return strFinal.toString();
	}

	public static String replaceStr(String str) {
		String srcStr[] = { "+", "*" };
		String desStr[] = { " or ", " and " };
		String changeStr = str;
		for (int i = 0; i < srcStr.length; i++) {
			StringBuffer tmpStr = new StringBuffer();
			int start = 0;
			String temp = srcStr[i];
			for (int end = changeStr.indexOf(temp, start); end > 0; end = changeStr
					.indexOf(temp, start)) {
				tmpStr.append(changeStr.substring(start, end));
				tmpStr.append(desStr[i]);
				start = end + 1;
			}

			tmpStr.append(changeStr.substring(start));
			changeStr = tmpStr.toString();
		}

		return changeStr;
	}

	public static String getStrHead(String str, String split) {
		int len = str.indexOf(split);
		if (len == -1)
			return str;
		else
			return str.substring(0, len);
	}

	public static long getLong(String num) {
		try {
			return Long.parseLong(num);
		} catch (Exception e) {
			log.info("StringUtil->getLong(String num) :" + e.toString());
		}

		return 0L;
	}

	public static int getInt(String num) {
		try {
			if (num == null || num.equals(""))
				return 0;

			return Integer.parseInt(num);
		} catch (Exception e) {
			log.info("StringUtil->getInt(String num):" + e.toString());
		}

		return 0;
	}

	public static String getString(String str, String defaultVal) {
		if (str == null)
			return defaultVal;
		else
			return str;
	}

	public static String getLinkStr(String ids[], String split) {
		StringBuffer str = new StringBuffer(1024);
		for (int i = 0; i < ids.length; i++)
			str.append("'" + ids[i] + "'").append(split);

		int len = str.length();
		if (len > 0)
			return str.toString().substring(0, len - 1);
		else
			return str.toString();
	}

	public static String getTagStr(String tagStr) {
		if (tagStr == null || tagStr.indexOf("{[") == -1
				|| tagStr.indexOf("]}") == -1) {
			return "";
		} else {
			tagStr = tagStr.substring(tagStr.indexOf("{[") + 2, tagStr
					.indexOf("]}"));
			return tagStr;
		}
	}

	public static int getContentNum(String content, String title) {
		if (content == null || title == null)
			return 0;
		int index = content.indexOf(title);
		if (index != -1)
			return index;
		index = content.indexOf(title.toUpperCase());
		if (index != -1)
			return index;
		else
			return 0;
	}

	public static int getContentLastNum(String content, String title) {
		if (content == null || title == null)
			return 0;
		int index = content.lastIndexOf(title);
		if (index != -1)
			return index;
		index = content.lastIndexOf(title.toUpperCase());
		if (index != -1)
			return index;
		else
			return 0;
	}

	public static String replaceString(String mainString, String oldString,
			String newString) {
		if (mainString == null)
			return "";
		if (oldString == null || oldString.length() == 0)
			return mainString;
		if (newString == null)
			return mainString;
		int i = mainString.lastIndexOf(oldString);
		if (i < 0)
			return mainString;
		StringBuffer mainSb = new StringBuffer(mainString);
		for (; i >= 0; i = mainString.lastIndexOf(oldString, i - 1))
			mainSb.replace(i, i + oldString.length(), newString);

		return mainSb.toString();
	}

	public static String replaceSubStr(String content, String subStr,
			String newStr) {
		if (content == null)
			return "";
		if (subStr == null || subStr.length() == 0)
			return content;
		if (newStr == null)
			return content;
		int i = getStrBefore(content, subStr);
		int j = getStrAfter(content, subStr);
		if (i <= 0 || j <= 0) {
			return content;
		} else {
			StringBuffer mainSb = new StringBuffer(content);
			mainSb.replace(i, j, newStr);
			return mainSb.toString();
		}
	}

	public static int getStrBefore(String content, String subStr) {
		int intSub = getContentNum(content, subStr);
		String subContent = content.substring(0, intSub);
		for (int i = subContent.length() - 1; i > 0; i--)
			if (subContent.charAt(i) == '=' || subContent.charAt(i) == '"'
					|| subContent.charAt(i) == '\'')
				return i + 1;

		return -1;
	}

	public static int getStrAfter(String content, String subStr) {
		int intSub = getContentNum(content, subStr);
		String subContent = content.substring(intSub);
		for (int i = intSub; i < content.length() - 1; i++)
			if (content.charAt(i) == '"' || content.charAt(i) == ' '
					|| content.charAt(i) == '\'')
				return i;

		return -1;
	}

	public static int[] getStringPlace(String content, String val) {
		HashSet set = new HashSet();
		for (int start = content.indexOf(val); start != -1; start = content
				.indexOf(val, start + 1))
			set.add(new Integer(start));

		Integer intStr[] = (Integer[]) set.toArray(new Integer[0]);
		int retInt[] = new int[intStr.length];
		for (int i = 0; i < intStr.length; i++)
			retInt[i] = intStr[i].intValue();

		return retInt;
	}

	public static int getKind(String content, String val1, String val2) {
		int first[] = getStringPlace(content, val1);
		int end[] = getStringPlace(content, val2);
		if (first.length == 0 || end.length == 0)
			return 0;
		for (int i = 0; i < first.length; i++) {
			for (int j = 0; j < end.length; j++)
				if (Math.abs(first[i] - end[j]) < 30)
					return end[j];

		}

		return 0;
	}

	public static String getStrBetween(String content, String first, String end) {
		if (content == null)
			return content;
		int fir = content.indexOf(first);
		int en = content.indexOf(end);
		if (first == null || fir == -1)
			return "";
		if (end == null || en == -1)
			return "";
		else
			return content.substring(fir + first.length(), en);
	}

	public static String[] getStrings(String content, String first,
			String second) {
		ArrayList list = new ArrayList();
		int fir = content.indexOf(first);
		for (int en = content.indexOf(second); fir != -1 && en != -1; en = content
				.indexOf(second)) {
			String sub = content.substring(fir + first.length(), en);
			list.add(sub);
			content = content.substring(en + second.length());
			fir = content.indexOf(first);
		}

		return (String[]) list.toArray(new String[0]);
	}

	public static String getStrs(String content, String first, String second) {
		String str[] = getStrings(content, first, second);
		if (str == null || str.length == 0)
			return "";
		StringBuffer strVal = new StringBuffer(200);
		for (int i = 0; i < str.length; i++)
			strVal.append(str[i]).append(",");

		return strVal.toString().substring(0, strVal.length() - 1);
	}

	public static String getBraceStr(String content, String sp) {
		int divide = content.indexOf(sp);
		if (divide == -1)
			return "error";
		String first = content.substring(0, divide);
		String second = content.substring(divide + 1);
		int before = getContentLastNum(first, "<span");
		int after = getContentNum(second, "</span>");
		if (before == -1 || after == -1)
			return "error";
		else
			return content.substring(before, divide + after
					+ "</span>".length() + 1);
	}

	public static final int countNum(String line, String oldString) {
		int counter = 0;
		if (line == null)
			return 0;
		int i = 0;
		if ((i = line.indexOf(oldString, i)) >= 0
				|| (i = line.indexOf(oldString.toLowerCase(), i)) >= 0) {
			counter++;
			char line2[] = line.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(line2.length);
			i += oLength;
			for (int j = i; (i = line.indexOf(oldString, i)) > 0; j = i) {
				counter++;
				i += oLength;
			}

			return counter;
		} else {
			return counter;
		}
	}

	public static String[] getDivideStr(String content, String split) {
		if (content == null)
			return new String[0];
		int index = content.indexOf(split);
		if (index == -1) {
			String strVal[] = new String[1];
			strVal[0] = content;
			return strVal;
		}
		int end = 0;
		int first = 0;
		ArrayList list = new ArrayList();
		while (index != -1) {
			end = index;
			String temp = content.substring(first, end);
			list.add(temp);
			index = content.indexOf(split, end + 1);
			first = end + split.length();
			end = index;
		}
		String tmp = content.substring(first);
		if (tmp != null && !tmp.trim().equals(""))
			list.add(tmp);
		return (String[]) list.toArray(new String[0]);
	}

	public static String getSingleTag(String content, String tag) {
		if (tag == null)
			return "";
		int len = content.indexOf(tag);
		if (len == -1)
			return "";
		StringBuffer str = new StringBuffer(100);
		for (int i = len; i < content.length(); i++) {
			char c = content.charAt(i);
			if (c == '>') {
				str.append(c);
				break;
			}
			str.append(c);
		}

		return str.toString();
	}

	public static String getInterceptHTML(String content, String starttag,
			String endtag, int myfilter) {
		String returnstr = "";
		if (content == null || starttag == null || endtag == null)
			return returnstr;
		int beginIndex = 0;
		int endIndex = 0;
		int len_tag = content.indexOf(starttag);
		if (len_tag == -1)
			return returnstr;
		int len_tag1 = content.indexOf(endtag, len_tag);
		endIndex = len_tag1 + 1;
		if (myfilter == 1)
			beginIndex = len_tag;
		if (myfilter == 2) {
			beginIndex = len_tag;
			endIndex = content.length();
		}
		returnstr = content.substring(beginIndex, endIndex);
		return returnstr;
	}

	public static String getInterceptHTMLIgnoreCase(String content,
			String starttag, String endtag, int myfilter) {
		if (content == null || starttag == null || endtag == null)
			return "";
		String returnstr = getInterceptHTML(content, starttag, endtag, myfilter);
		if (!"".equals(returnstr))
			return returnstr;
		String temp_tag = starttag.toLowerCase();
		int beginIndex = 0;
		int endIndex = 0;
		int len_tag = content.indexOf(starttag);
		len_tag = content.indexOf(temp_tag);
		if (len_tag == -1) {
			temp_tag = starttag.toUpperCase();
			len_tag = content.indexOf(temp_tag);
			if (len_tag == -1) {
				len_tag = content.toUpperCase().indexOf(temp_tag);
				if (len_tag == -1)
					return returnstr;
			}
		}
		int len_tag1 = content.indexOf(endtag, len_tag);
		endIndex = len_tag1 + 1;
		if (myfilter == 1)
			beginIndex = len_tag;
		if (myfilter == 2) {
			beginIndex = len_tag;
			endIndex = content.length();
		}
		returnstr = content.substring(beginIndex, endIndex);
		return returnstr;
	}

	public static String gotNull(String nullstring) {
		if (nullstring == null)
			nullstring = "";
		else if ("null".equals(nullstring.toLowerCase()))
			nullstring = "";
		else
			nullstring = nullstring.trim();
		return nullstring;
	}

	public static ArrayList HashMap2ArrayList(HashMap map) {
		ArrayList list = new ArrayList();
		String key;
		for (Iterator it = map.keySet().iterator(); it.hasNext(); list.add(map
				.get(key)))
			key = (String) it.next();

		return list;
	}

	public static ArrayList Set2ArrayList(Set map) {
		ArrayList list = new ArrayList();
		for (Iterator it = map.iterator(); it.hasNext(); list.add(it.next()))
			;
		return list;
	}

	public static ArrayList StringArry2List(String str[]) {
		ArrayList list = new ArrayList();
		for (int i = 0; i <= str.length - 1; i++)
			list.add(str[i]);

		return list;
	}

	public static ArrayList FormatForm(ArrayList list, int rowsNum) {
		ArrayList cols = new ArrayList();
		if (list != null) {
			ArrayList rows = null;
			int num = 0;
			for (int i = 0; i <= list.size() - 1; i++) {
				if (num == 0)
					rows = new ArrayList();
				num++;
				rows.add(list.get(i));
				if (num == rowsNum || i == list.size() - 1) {
					num = 0;
					cols.add(rows);
				}
			}

		}
		return cols;
	}

	public static String showFormat(String s, int num) {
		int from = 0;
		int to = 0;
		int count = 0;
		String temp = "";
		StringBuffer sb = new StringBuffer();
		if (s.length() > num) {
			count = s.length() / num;
			for (int i = 0; i < count; i++) {
				from = i * num;
				to = from + num;
				temp = s.substring(from, to);
				sb.append(temp).append("\n");
			}

			temp = s.substring(to, s.length());
			sb.append(temp).append("\n");
		} else {
			return s;
		}
		return sb.toString();
	}

	private static Logger log;
	private static HashMap hNum;
	private static String strVal[] = { "1", "2", "3", "4", "5", "6", "7", "8",
			"9", "0", "." };
	private static String flag[] = { "+", "-", " ", "\uFF0B", "\uFF0D" };
	private static String block[] = { "+", ",", "(", ")" };
	private static HashMap strMap;
	static Class class$0; /* synthetic field */

	static {
		log = Logger.getLogger(com.iphaeton.common.util.StringUtil.class);
		strMap = new HashMap();
		hNum = new HashMap();
		for (int i = 0; i < strVal.length; i++)
			hNum.put(strVal[i], strVal[i]);

		for (int i = 0; i < flag.length; i++)
			strMap.put(flag[i], flag[i]);

	}
}
